﻿$(function () {

    var oTable = new TableInit();
    oTable.Init();

    var oButtonInit = new ButtonInit();
    oButtonInit.Init();

    $("#btn_query").click(function () {
        TableRefresh("#tb_user");
    })
    $("#btn_clear").click(function () {
        ValueReset(["#search_datepicker input[name='start']", "#search_datepicker input[name='end']"]);
        TableRefresh("#tb_user");
    })
    //初始化表单验证
    $('#add_form').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            add_username: {
                validators: {
                    notEmpty: {
                        message: '用户名为必填项'
                    }
                }
            },
            add_accountnum: {
                validators: {
                    notEmpty: {
                        message: '账号为必填项'
                    }
                }
            },
            add_pwd: {
                validators: {
                    notEmpty: {
                        message: '密码为必填项'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度需要在6-30位之间'
                    }
                }
            },
            add_confirm_pwd: {
                validators: {
                    notEmpty: {
                        message: '确认密码为必填项'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度需要在6-30位之间'
                    },
                    identical: {
                        field: 'add_pwd',
                        message: '确认密码与密码不一致'
                    }
                }
            }
        }
    });
    $('#edit_form').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            edit_username: {
                validators: {
                    notEmpty: {
                        message: '用户名为必填项'
                    }
                }
            },
            edit_accountnum: {
                validators: {
                    notEmpty: {
                        message: '账号为必填项'
                    }
                }
            },
            edit_pwd: {
                validators: {
                    notEmpty: {
                        message: '密码为必填项'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度需要在6-30位之间'
                    }
                }
            },
            edit_confirm_pwd: {
                validators: {
                    notEmpty: {
                        message: '确认密码为必填项'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度需要在6-30位之间'
                    },
                    identical: {
                        field: 'edit_pwd',
                        message: '确认密码与密码不一致'
                    }
                }
            }
        }
    });
});

var TableInit = function () {
    var oTableInit = new Object();

    oTableInit.Init = function () {
        $('#tb_user').bootstrapTable({
            url: '../ashx/User.ashx',
            method: 'get',
            toolbar: '#toolbar',
            striped: true,
            cache: false,
            pagination: true,
            sortable: true,
            sortName: "CreateTime",
            sortOrder: "desc",
            queryParams: oTableInit.queryParams,
            sidePagination: "client",
            pageNumber: 1,
            singleSelect: true,
            pageSize: 10,
            pageList: [10, 25, 50, 100],
            search: true,
            strictSearch: false,
            showColumns: true,
            showRefresh: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            //height: 500,
            uniqueId: "ReportID",
            showToggle: true,
            cardView: false,
            detailView: false,
            showExport: true,
            buttonsAlign: "right",
            exportTypes: ['excel'],
            Icons: 'glyphicon-export',
            exportOptions: {
                ignoreColumn: [0],
                fileName: '用户',
                worksheetName: 'sheet1',
                tableName: '用户',
                excelstyles: ['background-color', 'color', 'font-size', 'font-weight']
            },
            columns: [{
                checkbox: true
            }, {
                field: 'UserID',
                title: '主键',
                visible: false
            }, {
                field: 'UserName',
                title: '姓名'
            }, {
                field: 'AccountNum',
                title: '账号'
            }, {
                field: 'PassWord',
                title: '密码',
                visible: false
            }, {
                field: 'Remark',
                title: '备注'
            }, {
                field: 'CreateTime',
                title: '创建时间'
            }]
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {
            action: "GetAllUser",
            username: $("#search_username").val()
        };
        return temp;
    };
    return oTableInit;
};

var ButtonInit = function () {
    var oInit = new Object();
    var postdata = {};

    oInit.Init = function () {
        $("#btn_add").click(function () {
            ValueReset(["#add_username", "#add_accountnum", "#add_pwd", "#add_confirm_pwd", "#add_remark"]);
            $("#Modal_add").modal("show");
            ResetForm("#add_form", true);
        })
        $("#btn_edit").click(function () {
            var rows = GetTableSelections("#tb_user");
            if (rows.length != 1) {
                ShowMsg("请选择一条需要修改的数据");
                return;
            } else {
                $("#edit_userid").val(rows[0].UserID);
                $("#edit_username").val(rows[0].UserName);
                $("#edit_accountnum").val(rows[0].AccountNum);
                $("#edit_pwd").val(rows[0].PassWord);
                $("#edit_confirm_pwd").val(rows[0].PassWord);
                $("#edit_remark").val(rows[0].Remark);
                $("#Modal_edit").modal("show");
                ResetForm("#edit_form", false);
            }
        })
        $("#btn_delete").click(function () {
            var rows = GetTableSelections("#tb_user");
            if (rows.length == 0) {
                ShowMsg("请选择要删除的数据！");
                return;
            } else {
                var IDsArray = new Array();
                rows.forEach(function (row) {
                    IDsArray.push(row.UserID);
                })
                $.ajax({
                    url: '../ashx/User.ashx',
                    data: { action: "DelUser", IDs: "'" + IDsArray.join("','") + "'" },
                    success: function (r) {
                        var ret = JSON.parse(r);
                        if (ret.code == 0) {
                            ShowMsg(ret.info, "success");
                            TableRefresh("#tb_user");
                        } else {
                            ShowMsg(ret.info, "error");
                        }
                    }
                });
            }
        })
    };
    return oInit;
};

function add_save() {
    if (!FormIsValid("#add_form")) {
        ShowMsg("表单验证不通过");
        return;
    }
    //var confirmpwd = $("#add_confirm_pwd").val();
    //var pwd = $("#add_pwd").val();
    //if (confirmpwd != pwd) {
    //    ShowMsg("两次输入的密码不一致");
    //    return;
    //}
    var obj = new Object();
    obj.UserID = $("#add_userid").val();
    obj.UserName = $("#add_username").val();
    obj.PassWord = $("#add_pwd").val();
    obj.AccountNum = $("#add_accountnum").val();
    obj.Remark = $("#add_remark").val()
    $.ajax({
        url: '../ashx/User.ashx?action=AddUser',
        data: { json: JSONFormat(obj, false) },
        success: function (r) {
            var ret = JSON.parse(r);
            if (ret.code == 0) {
                ShowMsg(ret.info, "success");
                TableRefresh("#tb_user");
                $("#Modal_add").modal("hide");
            } else {
                ShowMsg(ret.info, "error");
            }
        }
    });
}
function edit_save() {
    if (!FormIsValid("#edit_form")) {
        ShowMsg("表单验证不通过");
        return;
    }
    var obj = new Object();
    obj.UserID = $("#edit_userid").val();
    obj.UserName = $("#edit_username").val();
    obj.PassWord = $("#edit_pwd").val();
    obj.AccountNum = $("#edit_accountnum").val();
    obj.Remark = $("#edit_remark").val();
    //console.dir(obj);
    $.ajax({
        url: '../ashx/User.ashx?action=ModifyUser',
        data: { json: JSONFormat(obj, false) },
        success: function (r) {
            var ret = JSON.parse(r);
            if (ret.code == 0) {
                ShowMsg(ret.info, "success");
                TableRefresh("#tb_user");
                $("#Modal_edit").modal("hide");
            } else {
                ShowMsg(ret.info, "error");
            }
        }
    });
}